package pl.edu.pw.elka.tinyrepo.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Klasa obslugujaca wysylanie pliku.
 * 
 * @author Piotr Jarosik
 */
class SendHandler implements Runnable {
   private static final int BUFFER_SIZE = 256;
   private InputStream in;
   private OutputStream out;
   
   public SendHandler(InputStream in, OutputStream out) {
      super();
      this.in = in;
      this.out = out;
   }


   @Override
   public void run() {
      byte[] buffer = new byte[BUFFER_SIZE];
      int amount = 0;
      
      try {
         while((amount = in.read(buffer)) != -1) // pobieraj porcje o rozmiarze bufora, az dojdziesz do konca
            out.write(buffer, 0, amount);
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } // wyslij bufor do klienta.
      finally {
         try {
            out.close();
         }
         catch (Exception e) {
            e.printStackTrace();
         }
      }
   }
}
